package com.xuecheng.system.controller;

import com.xuecheng.commons.constants.CompanyStatusEnum;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.AuthInfo;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.commons.utils.JwtUtils;
import com.xuecheng.system.domain.Company;
import com.xuecheng.system.domain.User;
import com.xuecheng.system.service.UserService;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.system.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 教育机构前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CompanyController {

    @Autowired
    private CompanyService  companyService;


    // 机构查询
   /* @GetMapping("/company/mine")
    public ResponseResult mine(@RequestHeader("Authorization") String token){ // 1.接收token
        // 2.校验token
        Boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken){
            throw new BusinessException(ErrorCode.NOTTOKEN);
        }
        // 3.解析token
        AuthInfo authInfo = JwtUtils.getInfoFromToken(token);
        // 4.根据企业id查询数据库
        Company company = companyService.getById(authInfo.getCompanyId());
        // 5.封装并返回vo （复制工具类）
        CompanyVo vo = BeanHelper.copyProperties(company, CompanyVo.class);
        return ResponseResult.okResult(vo);
    }*/

    // 机构查询
    @GetMapping("/company/mine")
    public ResponseResult mine(){
        // 1.从线程内获取 authInfo
        AuthInfo authInfo = AuthInfoHolder.getAuthInfo();
        // 2.根据企业id查询数据库
        Company company = companyService.getById(authInfo.getCompanyId());
        // 3.封装并返回vo （复制工具类）
        CompanyVo vo = BeanHelper.copyProperties(company, CompanyVo.class);
        return ResponseResult.okResult(vo);
    }
    // 机构新增
    @PostMapping("/company")
    public ResponseResult save(@RequestBody Company company){
        company.setStatus(-1);
        company.setApprovalStatus(CourseConstants.AUDIT_UNPAST_STATUS);
        company.setCreateDate(new Date());
        companyService.save(company);
        return ResponseResult.okResult();
    }
    // 课程搜索
    @PostMapping("/company/list")
    public ResponseResult search(PageRequest params,@RequestBody Company company) {
        return companyService.find(params);
    }
    // 机构查询
    @GetMapping("/company/{id}")
    public ResponseResult findId(@PathVariable String id){

        return ResponseResult.okResult(companyService.findId(id));
    }
    @Autowired
    private UserService userService;
    // 机构审核
    @PostMapping("/company/approve")
    public ResponseResult findId(@RequestBody Map<String,String> p){
        User byId = userService.getById(AuthInfoHolder.getUserId());
        Company byId1 = companyService.getById(p.get("companyId"));
        byId1.setApprovalPeople(byId.getName());
        byId1.setApprovalDate(new Date());
        byId1.setApprovalNums(byId1.getApprovalNums()+1);
        byId1.setApprovalStatus(p.get("approvalStatus"));
        byId1.setApprovalComment(p.get("approvalComment"));
        companyService.updateById(byId1);
        return ResponseResult.okResult();
    }

}

